package com.wpc.smspringmvc.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpc.smspringmvc.dao.UserMapper;
import com.wpc.smspringmvc.model.Page;
import com.wpc.smspringmvc.model.User;
import com.wpc.smspringmvc.service.UserService;

/**
 * 业务服务实现层
 * @author wpc
 *
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 添加用户
	 * 
	 * @param user
	 * @throws Exception 
	 */
	@Override
	public int addUser(User user) throws Exception {
		
		return userMapper.addUser(user);
	}

	/**
	 * 编辑用户
	 * 
	 * @param user
	 */
	@Override
	public void editorUser(User user) throws Exception{
		
		userMapper.editorUser(user);
	}

	/**
	 * 删除用户
	 * 
	 * @param user
	 */
	@Override
	public void deleteUser(User user) throws Exception{
		
		userMapper.deleteUser(user);
	}

	/**
	 * 根据编号查询用户
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public User getUserById(User user) throws Exception{
		
		return userMapper.getUserById(user);
	}

	/**
	 * 查询用户
	 * 
	 * @param user
	 * @return
	 */
	@Override
	public Page getUsers(Map<String, Object> condition) throws Exception{
		
		Page page=new Page();
		
		int pageSize = Integer.parseInt(String.valueOf(condition.get("pageSize")));
		page.setPageSize(pageSize);
		
		int pageIndex = Integer.parseInt(String.valueOf(condition.get("pageIndex")));
		page.setPageIndex(pageIndex);
		
		page.setTotal(userMapper.getUsersTotal(condition));
		
		page.setPageCount(page.getTotal()/pageSize+(page.getTotal()%pageSize==0?0:1));
		
		page.setUserList(userMapper.getUsers(condition));
		
		return page;
	}

}
